package com.weishuang.monitor.leader.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.weishuang.monitor.leader.commons.entity.BaseEntity;
import com.weishuang.monitor.leader.commons.entity.Result;
import com.weishuang.monitor.leader.commons.enums.SCODE;
import com.weishuang.monitor.leader.commons.utils.ResultUtils;
import com.weishuang.monitor.leader.entity.SysDictData;
import com.weishuang.monitor.leader.entity.dto.DictDataDTO;
import com.weishuang.monitor.leader.service.ISysDictDataService;
import com.weishuang.monitor.leader.service.SysCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 数据字典数据 前端控制器
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/leader/sysDictData")
public class SysDictDataController {

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private SysCacheService sysCacheService;

    /**
     * 分页查询数据字典列表
     * @param dictType
     * @param dictDataDTO
     * @return
     */
    @GetMapping("listPageDictData/{dictType}")
    public Result listPageDictData(@PathVariable String dictType, DictDataDTO dictDataDTO) {
        dictDataDTO.setDictType(dictType);
        Page result = sysDictDataService.listPageDictData(dictDataDTO);
        return ResultUtils.success(result);
    }


    /**
     * 新增字典数据
     * @param sysDictData
     * @return
     */
    @PostMapping
    public Result addDictData(@Validated SysDictData sysDictData) {
        sysDictData.setDictCode(IdWorker.getIdStr());
        sysDictData.insertInit();
        sysDictDataService.save(sysDictData);
        sysCacheService.clearDict();
        return ResultUtils.success();
    }

    @PutMapping("/{code}")
    public Result updateDictDate(@PathVariable String code, @Validated SysDictData sysDictData) {
        sysDictData.setDictCode(code);
        sysDictData.updateInit();
        sysDictDataService.updateById(sysDictData);
        sysCacheService.clearDict();
        return ResultUtils.success();
    }

    @DeleteMapping("/{code}")
    public Result deleteDictDate(@PathVariable String code) {
        LambdaUpdateWrapper<SysDictData> eq = Wrappers.lambdaUpdate(SysDictData.class).set(BaseEntity::getStatus, SCODE.STATUS_DELETE.getValue())
                .eq(SysDictData::getDictCode, code);
        sysDictDataService.update(eq);
        sysCacheService.clearDict();
        return ResultUtils.success();
    }

    /**
     * 前端获取数据字典
     * @return
     */
    @GetMapping("getDict")
    public Result getDict() {
        Map<String, Object> result = sysCacheService.getDict();
        return ResultUtils.success(result);
    }


}
